package com.lxyk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.lxyk.mapper.HistoryMapper;
import com.lxyk.pojo.History;
import com.lxyk.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class HistoryServiceImpl implements HistoryService {
    @Autowired
    private HistoryMapper historyMapper;
    /*bug:空指针异常 由于我没有加@Autowired导致没有装配HistoryMapper对象导致空指针异常*/
    @Override
    public List<History> queryHistory() {
        List<History> historyList = historyMapper.selectList(null);
        return historyList;
    }

    @Override
    public PageInfo<History> listHistory(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        List<History> historyList = historyMapper.listHistory(map);
        return new PageInfo<>(historyList);
    }

    @Override
    public int saveHistory(History history) {
        return historyMapper.saveHistory(history);
    }

    @Override
    public History getHistoryById(Integer hid) {
        return historyMapper.getHistoryById(hid);
    }

    @Override
    public int updateHistory(History history) {
        return historyMapper.updateHistory(history);
    }

    @Override
    public int batchDeleteHistory(String idsList) {
        String[] split = StrUtil.split(idsList, ",");
        List<Integer> ids=new ArrayList<>();
        for (String id : split) {
            if (StrUtil.isNotEmpty(id)){
                ids.add(Integer.parseInt(id));
            }
        }
        return historyMapper.batchDeleteHistory(ids);
    }
}
